package net.sunofbeach.blog.services.impl;

import lombok.extern.slf4j.Slf4j;
import net.sunofbeach.blog.dao.LyDao;
import net.sunofbeach.blog.pojo.Ly;
import net.sunofbeach.blog.response.ResponseResult;
import net.sunofbeach.blog.services.ILyService;
import net.sunofbeach.blog.utils.Constants;
import net.sunofbeach.blog.utils.IdWorker;
import net.sunofbeach.blog.utils.TextUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.Date;

@Slf4j
@Service
@Transactional
public class LyServiceImpl extends BaseService implements ILyService {

    @Autowired
    private IdWorker idWorker;

    @Autowired
    private LyDao lyDao;

    @Autowired
    private TaskService taskService;

    @Override
    public ResponseResult postLy(Ly ly) {
        //检查内容，必须有的
        //1.昵称2.邮箱3.留言内容4.对方网站
        String nickname = ly.getNickname();
        if (TextUtils.isEmpty(nickname)) {
            return ResponseResult.FAILED("留言昵称不可以为空");
        }
        String email = ly.getEmail();
        if (TextUtils.isEmpty(email)) {
            return ResponseResult.FAILED("留言邮箱不可以为空");
        }
        String content = ly.getContent();
        if (TextUtils.isEmpty(content)) {
            return ResponseResult.FAILED("留言内容不可以为空");
        }

        String orderUrl = ly.getOrderUrl();
        if ((!TextUtils.isEmpty(orderUrl)) && (!orderUrl.contains("http"))) {
            return ResponseResult.FAILED("网站URL需要包含有https或者http的全域名");
        }

        //如果是登录用户，那么就直接获得原来的头像
        String avatar = ly.getAvatar();
        if (TextUtils.isEmpty(avatar)) {
            ly.setAvatar(Constants.User.DEFAULT_AVATAR);
        }
        //补全内容
        //1.头像（默认，或者多给几个随机）2.id 3.创建时间


        //补全内容
        //1.头像（默认，或者多给几个随机）2.id 3.创建时间

        ly.setState("1");
        ly.setId(idWorker.nextId() + "");

        ly.setCreateTime(new Date());
        lyDao.save(ly);
        //邮件提醒小郭同学
        try {
            //TODO:后续出售改这里
            taskService.sendCommentNotify(nickname,content,"1292196066@qq.com");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseResult.SUCCESS("留言成功");
    }

    @Override
    public ResponseResult listLys(int page, int size) {
        page = checkPage(page);
        size = checkSize(size);
        //没有就往下走
        Sort sort = new Sort(Sort.Direction.DESC, "createTime");
        Pageable pageable = PageRequest.of(page - 1, size, sort);
        Page<Ly> all = lyDao.findAll(pageable);
        return ResponseResult.SUCCESS("获取留言列表成功").setData(all);

    }

    @Override
    public ResponseResult getLyCount() {
        long count = lyDao.count();
        return ResponseResult.SUCCESS("父评论总数获取成功").setData(count);
    }


}

